package com.dqyt.khjc.controller;

import com.dqyt.khjc.bean.ResultMsg;
import com.dqyt.khjc.bean.TouristRole;
import com.dqyt.khjc.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 获取角色列表  在用户管理的 新增用户使用
     * @param request
     * @return
     */
    @RequestMapping(value = "/api/getRoleList", method = {RequestMethod.GET,RequestMethod.POST})
    public ResultMsg getRoleList(HttpServletRequest request){
        ResultMsg resultMsg =roleService.getRoleList(request);
        return resultMsg;
    }



    /**
     * 获取角色列表  在用户管理的  用户一览使用
     * @param request
     * @return
     */
    @RequestMapping(value = "/api/getRoleList2", method = {RequestMethod.GET,RequestMethod.POST})
    public ResultMsg getRoleList2(HttpServletRequest request){
        ResultMsg resultMsg =roleService.getRoleList2(request);
        return resultMsg;
    }

    /**
     * 获取角色列表 用户管理 权限配置
     * @param request
     * @return
     */
    @RequestMapping(value = "/api/getRoleList3", method = {RequestMethod.GET,RequestMethod.POST})
    public ResultMsg getRoleList3(HttpServletRequest request){
        ResultMsg resultMsg =roleService.getRoleList3(request);
        return resultMsg;
    }





    /**
     * 分页查询 角色信息
     * @param request
     * @param page
     * @param size
     * @param roleName
     * @return
     */
    @RequestMapping(value = "/api/getRoleListPage", method = {RequestMethod.GET,RequestMethod.POST})
    public ResultMsg getRoleListPage(HttpServletRequest request,
                                     @RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "10") Integer size,
                                     @RequestParam(name = "roleName", required = false, defaultValue = "") String roleName){
        ResultMsg resultMsg = roleService.getRoleListPage(request,page,size,roleName);
        return resultMsg;
    }

    /**
     * 添加角色
     * @param request
     * @param roleName
     * @param rolelBz
     * @return
     */
    @RequestMapping(value = "/api/addRole", method = {RequestMethod.POST})
    public ResultMsg addRole(HttpServletRequest request, String  roleName,String rolelBz){
        ResultMsg resultMsg = roleService.addRole(request,roleName,rolelBz);
        return resultMsg;
    }



    /**
     * 登陆限制
     * @param roleId 角色id
     * @param loginStatue  0:限制登录 1:允许登陆
     * @return
     */
    @RequestMapping(value = "/api/loginRoleRes", method = {RequestMethod.GET,RequestMethod.POST})
    public ResultMsg loginRoleRestrictions(HttpServletRequest request,long roleId,int loginStatue,String roleName){
        ResultMsg resultMsg = roleService.loginRoleRes(request,roleId,loginStatue,roleName);
        return resultMsg;
    }



    /**
     * 删除角色
     * @param roleId 角色id
     * @return
     */
    @RequestMapping(value = "/api/deleteRoler", method = {RequestMethod.GET,RequestMethod.POST})
    public ResultMsg deleteRoler(HttpServletRequest request,long roleId,String roleName){
        ResultMsg resultMsg = roleService.deleteRoler(request,roleId,roleName);
        return resultMsg;
    }



    /**
     * 设置角色权限
     * @return
     */
    @RequestMapping(value = "/api/setPermission", method = {RequestMethod.GET,RequestMethod.POST})
    public ResultMsg setPermission(HttpServletRequest request,String roleJson){
        ResultMsg resultMsg = roleService.setPermission(request,roleJson);
        return resultMsg;
    }



    /**
     * 修改角色的名称和备注
     * @return
     */
    @RequestMapping(value = "/api/updateRoler", method = {RequestMethod.GET,RequestMethod.POST})
    public ResultMsg updateRoler(HttpServletRequest request,long roleId ,String roleName,String  rolelBz){
        ResultMsg resultMsg = roleService.updateRoler(request,roleId,roleName,rolelBz);
        return resultMsg;
    }


    /**
     * 获取游客权限
     */
    @GetMapping("/api/touristRoles")
    public TouristRole getTouristRoles(){
      return   roleService.getTouristRoles();
    }
    /**
     * 更新游客权限
     */
    @PostMapping("/api/UptouristRoles")
    public ResultMsg UpdateTouristRoles(HttpServletRequest request,String[] mz,String[] qp,String[] ylb){
        String mzStr = arrChangeString(mz);
        String qpStr = arrChangeString(qp);
        String ylbStr = arrChangeString(ylb);

        return  roleService.updateTouristRole(request,mzStr,qpStr,ylbStr);
    }

    /**
     * 更新用户权限
     */
    @PostMapping("/api/upCustomeRole")
    public ResultMsg upCustomeRole(HttpServletRequest request,String[] role, int customerId){
        String roleStr = arrChangeString(role);
        return  roleService.upCustomeRole(request,roleStr,customerId);
    }

    //数组转String
    public String arrChangeString(String[] args){
        String str = "";
        if (args.length>0){
            for (int i= 0;i<args.length;i++) {
                str+=args[i]+",";
            }
            str = str.substring(0,str.length()-1);
            System.out.println(str);
        }else {
            str="0";
        }
        return str;
    }
}
